package com.ruoyi.quality.controller.web;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.ListType;
import com.ruoyi.common.core.domain.vo.ListItemVo;
import com.ruoyi.common.core.domain.vo.QuestionsVo;
import com.ruoyi.safe.domain.ListConstruction;
import com.ruoyi.safe.domain.ListDevise;
import com.ruoyi.safe.domain.ListMaintenance;
import com.ruoyi.safe.domain.ListManage;
import com.ruoyi.safe.service.IListTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quality.domain.TQualityQuestions;
import com.ruoyi.quality.service.ITQualityQuestionsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问题规范库Controller
 *
 * @author lxg
 * @date 2024-02-02
 */
@RestController
@RequestMapping("/quality/questions")
public class TQualityQuestionsController extends BaseController
{
    @Autowired
    private ITQualityQuestionsService tQualityQuestionsService;
    @Autowired
    private IListTypeService listTypeService;
    /**
     * 查询问题规范库列表
     */
    @PreAuthorize("@ss.hasPermi('quality:questions:list')")
    @GetMapping("/list")
    public TableDataInfo list(TQualityQuestions tQualityQuestions)
    {
        startPage();
        List<TQualityQuestions> list = tQualityQuestionsService.selectTQualityQuestionsList(tQualityQuestions);
        return getDataTable(list);
    }

    /**
     * 导出问题规范库列表
     */
    @PreAuthorize("@ss.hasPermi('quality:questions:export')")
    @Log(title = "问题规范库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TQualityQuestions tQualityQuestions)
    {
        List<TQualityQuestions> list = tQualityQuestionsService.selectTQualityQuestionsList(tQualityQuestions);
        ExcelUtil<TQualityQuestions> util = new ExcelUtil<TQualityQuestions>(TQualityQuestions.class);
        util.exportExcel(response, list, "问题规范库数据");
    }

    /**
     * 获取问题规范库详细信息
     */
    @PreAuthorize("@ss.hasPermi('quality:questions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tQualityQuestionsService.selectTQualityQuestionsById(id));
    }

    /**
     * 新增问题规范库
     */
    @PreAuthorize("@ss.hasPermi('quality:questions:add')")
    @Log(title = "问题规范库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TQualityQuestions tQualityQuestions)
    {
        return toAjax(tQualityQuestionsService.insertTQualityQuestions(tQualityQuestions));
    }

    /**
     * 修改问题规范库
     */
    @PreAuthorize("@ss.hasPermi('quality:questions:edit')")
    @Log(title = "问题规范库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TQualityQuestions tQualityQuestions)
    {
        return toAjax(tQualityQuestionsService.updateTQualityQuestions(tQualityQuestions));
    }

    /**
     * 删除问题规范库
     */
    @PreAuthorize("@ss.hasPermi('quality:questions:remove')")
    @Log(title = "问题规范库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tQualityQuestionsService.deleteTQualityQuestionsByIds(ids));
    }

    /**
     * 获取质量问题库信息（带分类）
     */
    @Log(title = "获取质量问题库信息（带分类）")
    @GetMapping("/questionList")
    public AjaxResult questionList()
    {
        ListType listType = new ListType();
        listType.setStatus("0");
        List<ListType> archivesTypes = listTypeService.selectListTypeList(listType);
        List<TQualityQuestions> listDevises = tQualityQuestionsService.selectTQualityQuestionsList1();
        for (int i = 0; i < archivesTypes.size(); i++) {
                int index = i;
                List<TQualityQuestions> collect = listDevises.stream().filter(c -> archivesTypes.get(index).getId().toString().equals(c.getTypeNo())).collect(Collectors.toList());
                List<QuestionsVo> listItemVos = new ArrayList<>();
                for (TQualityQuestions listDevise : collect) {
                    QuestionsVo questionsVo = QuestionsVo.builder().build();
                    BeanUtils.copyProperties(listDevise, questionsVo);
                    questionsVo.setId(new SecureRandom().nextInt()+questionsVo.getId());
                    listItemVos.add(questionsVo);
                }
                archivesTypes.get(i).setQuestionChildren(listItemVos);
        }
        List<TreeSelect> treeSelects = listTypeService.buildTypeTreeSelect(archivesTypes);
        return AjaxResult.success(treeSelects);
    }
}
